﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Rmlife.Site;
using Rmlife.Config;
using Rmlife.Common;
using Rmlife.Control;
using Rmlife.Model;

namespace Rmlife.Web
{
    public partial class MarkList :PageBase
    {
        #region 搜索组合
        private string strWhere = "";
        /// <summary>
        /// 排序条件
        /// </summary>
        private string strOrder = "";
        #endregion

        #region 搜索属性
        private string eTitle { get; set; }

        private string eAuthor { get; set; }

        private string eStartDate { get; set; }

        private string eEndDate { get; set; }
        #endregion

        #region 搜索关键字
 
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            string markID = WebReq.GetString("markID");
            string mode = WebReq.GetString("mode");
            string markUrl = emarkUrl.Text.ToString();
            string title = etitle.Text.ToString();
            string author = eauthor.Text.ToString();
            if (mode != "")
            {
                try
                {
                    if ("edit" == mode)
                    {
                        Bookmarks.UpdUrlByID(int.Parse(markID), markUrl, title, author);
                        Response.Write("<script type=\"text/javascript\">top.location.href='" + BaseConfigs.GetSitePath + "';</script>");
                        return;
                    }
                    if ("add" == mode)
                    {
                        BookmarkInfo info = new BookmarkInfo();
                        info.Title = etitle.Text.ToString().Trim();
                        info.Author = eauthor.Text.ToString().Trim();
                        info.BookmarkUrl = emarkUrl.Text.ToString().Trim();
                        info.DateCreated = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                        Bookmarks.AddBookmarkInfo(info);
                        Response.Write("<script type=\"text/javascript\">top.location.href='" + BaseConfigs.GetSitePath + "';</script>");
                        return;
                    }
                }
                catch(Exception ex)
                { 
                    Response.Write("<script>alert('"+ex.Message+"')</script>");
                }
            }
            else
            {
                if (!IsPostBack)
                {
                    txtStartDate.Text = DateTime.Now.AddMonths(-1).ToString();
                    txtEndDate.Text = DateTime.Now.AddDays(1).ToString();
                    if (WebReq.GetString("eTitle") != "") this.txtTitle.Text = WebReq.GetString("eTitle");
                    if (WebReq.GetString("eAuthor") != "") this.txtAuthor.Text = WebReq.GetString("eAuthor");
                    if (WebReq.GetString("eStartDate") != "") this.txtStartDate.Text = WebReq.GetString("eStartDate");
                    if (WebReq.GetString("eEndDate") != "") this.txtEndDate.Text = WebReq.GetString("eEndDate");
                    BindGrid();
                }
            }
        }
        private void BindGrid()
        {
          //  strWhere += WebUtils.GetPageSplitWhere(this.txtStartDate, this.txtEndDate, WebReq.GetString("eStartDate"), WebReq.GetString("eEndDate"), "dt", ref _keywordDateBegin, ref _keywordDateEnd);
            if (!string.IsNullOrEmpty(txtStartDate.Text) && !string.IsNullOrEmpty(txtEndDate.Text))
            {
                if (Convert.ToDateTime(txtStartDate.Text.ToString().Trim()) < Convert.ToDateTime(txtEndDate.Text.ToString().Trim()))
                {
                    strWhere += " and DateCreated >= '" + txtStartDate.Text.ToString().Trim() + "' and DateCreated < '" + Convert.ToDateTime(txtEndDate.Text.ToString().Trim()).AddDays(1) + "'";
                }
                else
                {
                    base.Execute_JS("", "alert('开始日期不能大于结束日期');window.location='MarkList.aspx';");
                    return;
                }
            }
               
            if (!string.IsNullOrEmpty(txtTitle.Text.Trim()))
            {
                strWhere += " and Title like '%" + txtTitle.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtAuthor.Text.Trim()))
            {
                strWhere += " and Author like '%" + txtAuthor.Text.Trim() + "%'";
            }

            #region 排序条件
            strOrder="BookmarkID";
            if (this.ViewState["SortExpression"]!=null && this.SortExpression!="")
            {
                strOrder = this.SortExpression;
            }
            #endregion
            DataGrid1.DataSource = Bookmarks.GetBookMarkList(page1.CurrentPageIndex, page1.PageSize, strWhere, strOrder);
            DataGrid1.DataBind();
            int totalcount = Bookmarks.QueryBookmarkCnt(strWhere);
            page1.RecordCount = totalcount;
            this.page1.CustomInfoHTML = string.Concat(new object[] { "第", this.page1.CurrentPageIndex, "/", this.page1.PageCount, "页&nbsp;，共<font color='#ff0000'><strong>", totalcount, "</strong></font>条&nbsp;" });

        }

        /// <summary>
        /// 处理翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void page1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        #region 排序
        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.DataGrid1.SortCommand += new Rmlife.Control.DataGrid.RecssDataGridSortCommandEventHandler(this.SortGrid1_SortCommand);
        }
        private void SortGrid1_SortCommand(object sender, Rmlife.Control.DataGrid.RecssDataGridSortCommandEventArgs e)
        {
            this.ViewState["SortExpression"] = e.SortExpression;
            BindGrid();
        }

        private string SortExpression
        {
            get { return ViewState["SortExpression"] == null ? string.Empty : ViewState["SortExpression"].ToString(); }
            set { this.ViewState["SortExpression"] = value; }
        }
        #endregion
        /// <summary>
        /// 处理搜索事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BtnSearch_Click(object sender, EventArgs e)
        {
            this.page1.CurrentPageIndex = 1;
            BindGrid();
        }
        /// <summary>
        /// 处理导出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BtnExport_Click(object sender, EventArgs e)
        {
            for (int i = this.DataGrid1.Columns.Count - 1; i >= 0; i--)
                this.DataGrid1.Columns[i].SortExpression = "";

            this.page1.CurrentPageIndex = 1;
            this.page1.PageSize = 0;
            BindGrid();
            WebUtils.ExportDataToExcel(DataGrid1);
            return;
        }

        protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                int markID = int.Parse((e.Item.FindControl("hidID") as HtmlInputHidden).Value.ToString().Trim());
                if (e.CommandName == "deleteRecord")
                {
                    if (Bookmarks.DelBookmarkInfo(markID))
                    {
                        Response.Write("<script>alert('删除成功')</script>");
                        base.RegisterStartupScript("PAGE", "window.location.href='" + WebReq.GetPageName() + "';");
                    }
                    else
                    {
                        Response.Write("<script>alert('删除失败')</script>");
                        base.RegisterStartupScript("PAGE", "window.location.href='" + WebReq.GetPageName() + "';");
                    }

                    base.RegisterStartupScript("PAGE", "window.location.href='MarkList.aspx';");
                }
            }
            catch
            {
            }
        }
    }
}
